package com.ruike.alisurface.ui.setting;

import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.alipay.iot.sdk.APIManager;
import com.alipay.iot.sdk.coll.CollectionAPI;
import com.alipay.iot.sdk.coll.TradeDataConstants;
import com.alipay.iot.sdk.payment.PaymentAPI;
import com.alipay.zoloz.smile2pay.service.Zoloz;
import com.alipay.zoloz.smile2pay.service.ZolozCallback;
import com.cunoraz.gifview.library.GifView;
import com.ruike.alisurface.BuildConfig;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.AlipayBean;
import com.ruike.alisurface.bean.ShopDetailBean;
import com.ruike.alisurface.http.MchApiHttp;
import com.ruike.alisurface.http.UrlHttpUtils;
import com.ruike.alisurface.http.WxPayHttp;
import com.ruike.alisurface.ui.dialog.CustomDialog;
import com.ruike.alisurface.ui.payPage.PayForQRCodeActivity;
import com.ruike.alisurface.utils.MYUtiles;
import com.ruike.alisurface.utils.MyCountDownTimer;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;
import com.voodoo.lib_wechatpayface.ResultDataBean;
import com.voodoo.lib_wechatpayface.WeChatPayFace;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

import static com.alipay.iot.sdk.coll.TradeDataConstants.TradeFailType.NETWORK_EXCEPTION;
import static com.alipay.iot.sdk.coll.TradeDataConstants.TradeFailType.NONE;

/**
 * Author: voodoo
 * CreateDate: 2020-04-08
 * Description: 微信刷脸测试界面
 */
public class SettingTestCameraActivity extends BaseAty implements WeChatPayFace.OnWeChatSdkListener {

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pay_face;
    }

    @Override
    protected void initViews() {
        L.i("测试微信刷脸==" + this.getClass().getName());
    }

    @Override
    protected void initData() {
        new MyCountDownTimer(2000, 2000) {

            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                startWeChatPayFace();
            }
        }.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void startWeChatPayFace() {
        WeChatPayFace.getInstance().setOnWeChatSdkListener(this);
        WeChatPayFace.getInstance().getRawdata(); // 获取 RawData 获取到 RawData 之后上传后台
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WeChatPayFace.getInstance().setOnWeChatSdkListener(null);
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
        if (UrlHttpUtils.GET_AUTH_INFO_URL.equals(requestUrl)) { // 上传RawData返回
            try {
                JSONObject jsonObject = new JSONObject(requestJsonStr);
                ResultDataBean dataBean = GsonUtils.json2Model(jsonObject.getJSONObject("data").toString(), ResultDataBean.class);
                // 拿到返回值之后 使用回传的参数 调取摄像头，获取人脸凭证
                WeChatPayFace.getInstance().openCamera(dataBean, "123456789000", "0.01");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        super.onError(requestUrl, errorMsg);
        showErrorTip(errorMsg);
    }

    // 微信刷脸测试

    @Override
    public void weChatLog(String logMsg) {
        L.iTag("微信刷脸Log", logMsg);
    }

    @Override
    public void weChatSdkToast(String toastMsg) {
        showToast(toastMsg);
        if ("微信SDK未初始化成功".equals(toastMsg)) {
            startCountdownTimer(2, null, SettingActivity.class);
        }
    }

    @Override
    public void getRawData(String rawData) {
        L.i("获取到RawData，并上传");
        WxPayHttp.getAuthinfo(rawData, this, true);
    }

    @Override
    public void getFaceCode(String userOrderId, String faceCode, String openid) {
        // 拿到人脸凭证上传给后台 然后等待支付结果
        WxPayHttp.createMicropay(userOrderId, faceCode, this);
    }

//    @Override
//    public void wxPayResult(boolean isPaySuccess) {
//        // 获取到支付结果之后，直接开始轮询后台，去查询后台的支付结果
////        pollingPayResult();
//        L.i("测试微信刷脸 支付" + (isPaySuccess ? "成功" : "失败"));
//        startCountdownTimer(2, null, SettingActivity.class);
//    }

    @Override
    public void wxPayCancel(boolean isSuccess, String msg) {
        showErrorTip(msg);
        L.i("测试微信刷脸失败" + " --" + msg);
        startCountdownTimer(2, null, SettingActivity.class);
    }

}
